﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SsrsRenderStudio.Shared;
using SsrsRenderStudio.Configuration;
using SsrsRenderStudio.Renderer.Output;
using SsrsRenderStudio.KnownTypesAndModels;

namespace SsrsRenderStudio.Wcf.Services
{
    public static class RexKeepAlive
    {
        private static System.Timers.Timer _rexKeepAliveTimer = new System.Timers.Timer();
        private static TimeSpan _rexKeepAliveInterval = new TimeSpan(0, 30, 0);

        private static bool _timerSet = false;
        private static void SetTimer(bool force = false)
        {
            if (!_timerSet || force)
            {
                _timerSet = true;

                RenderStudioServerConfig10 si = ConfigurationManager.ServerInfoFromFriendlyName10(ConfigurationManager.ApplicationConfigFromConfigPointer.ApplicationName);

                if (si != null)
                {
                    _rexKeepAliveInterval = si.RexKeepAliveTimer;
                }
            }

            if (!_rexKeepAliveTimer.Enabled)
            {
                Logger.Log("70000");
                _rexKeepAliveTimer.Interval = _rexKeepAliveInterval.TotalMilliseconds;
                _rexKeepAliveTimer.Elapsed += new System.Timers.ElapsedEventHandler(_Timer_Elapsed);
                _rexKeepAliveTimer.AutoReset = true;
                _rexKeepAliveTimer.Start();
            }
        }

        static void _Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            keepAlive();
        }

        private static List<ReportServerConfig10> _rexs = new List<ReportServerConfig10>();

        public static void Add(ReportServerConfig10 rexInfo)
        {
            SetTimer();

            if (_rexs.Find(delegate(ReportServerConfig10 rex) { return rex.FriendlyName == rexInfo.FriendlyName; }) == null)
            {
                _rexs.Add(rexInfo);
                Logger.Log("70001", rexInfo.FriendlyName);

            }
        }

        /// <summary>
        /// Hit each REX with a dummy render request.
        /// </summary>
        private static void keepAlive()
        {
            Logger.StartSection("keepalive_penalty");
            Logger.Log("70002");
            Renderer.RenderingEngine renderer = new Renderer.RenderingEngine();

            foreach (var r in _rexs)
            {
                byte[][] a; string b; string c;
                renderer.ChangeContext(null, System.Text.Encoding.ASCII.GetBytes(Properties.Resources.RexKeepAliveDummyDefinition), r.RexUrl);
                EnumServiceSideException exception =  renderer.RenderReport(out a, out b, out c);

                if (exception == EnumServiceSideException.NONE)
                {
                    Logger.Log("70003", r.FriendlyName);
                }
                else
                {
                    Logger.Log("10003",  r.FriendlyName ,exception.ToString(), renderer.ThrownException);
                }
            }
            
            renderer = null;

            Logger.LogSection("70004", "keepalive_penalty");
        }
    }
}
